package com.hoow.shakoow.components.button

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import android.widget.RelativeLayout
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentManager
import com.hoow.shakoow.R
import com.hoow.shakoow.base.BaseFormAction
import com.hoow.shakoow.databinding.ButtonSubmitBinding
import com.hoow.shakoow.extend.addBoldStyle
import com.hoow.shakoow.extend.addNormalStyle

/**
<declare-styleable name="SubmitButton">
<attr name="cwsbText" format="string" />
<attr name="cwsbTextSize" format="dimension" />
<attr name="cwsbTextColor" format="color" />
<attr name="cwsbBackgroundRes" format="reference" />
<attr name="cwsbIsCurrencyUnitHidden" format="boolean" />
<attr name="cwsbIsBold" format="boolean" />
</declare-styleable>
 * */
class NextButton : RelativeLayout, BaseFormAction {

    lateinit var mViewBinding: ButtonSubmitBinding
    private lateinit var vRootView: View

    private var cwsbTextColor: Int = 0
    private var cwsbTextSize: Float = 0F
    private var cwsbText: String? = null
    private var cwsbBackgroundRes: Int = 0
    private var cwsbIsCurrencyUnitHidden: Boolean = true
    private var cwsbIsBold: Boolean = true

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        attrs?.let {
            context?.let { cxt ->
                val array =
                    cxt.obtainStyledAttributes(attrs, R.styleable.NextButton)
                cwsbTextColor = array.getColor(
                    R.styleable.NextButton_cwsbTextColor,
                    ContextCompat.getColor(cxt, R.color.white)
                )

                cwsbText =
                    array.getString(R.styleable.NextButton_cwsbText)

                cwsbTextSize =
                    array.getDimension(
                        R.styleable.NextButton_cwsbTextSize,
                        resources.getDimensionPixelSize(R.dimen.sp_16).toFloat()
                    )

                cwsbBackgroundRes =
                    array.getResourceId(
                        R.styleable.NextButton_cwsbBackgroundRes,
                        R.drawable.ai_submit_checked
                    )

                cwsbIsCurrencyUnitHidden = array.getBoolean(
                    R.styleable.NextButton_cwsbIsCurrencyUnitHidden,
                    true
                )

                cwsbIsBold = array.getBoolean(
                    R.styleable.NextButton_cwsbIsBold,
                    true
                )
                array.recycle()
            }
        }
        context?.let {
            vRootView = View.inflate(it, R.layout.button_submit, this)
            mViewBinding =
                ButtonSubmitBinding.bind(vRootView)
            initSubviews()
        }
    }

    override fun initSubviews() {
        try {
            mViewBinding.textSubmitButton.text = (cwsbText)
            mViewBinding.textSubmitButton.setTextColor(cwsbTextColor)
            mViewBinding.textSubmitButton.setTextSize(
                TypedValue.COMPLEX_UNIT_PX,
                cwsbTextSize
            )
            if (cwsbIsBold) {
                mViewBinding.textSubmitButton.addBoldStyle()
            } else {
                mViewBinding.textSubmitButton.addNormalStyle()
            }

            mViewBinding.rlSubmitButtonRoot.setBackgroundResource(cwsbBackgroundRes)

            if (cwsbIsCurrencyUnitHidden) {
                mViewBinding.imageSubmitButtonNextArrow.visibility = GONE
            } else {
                mViewBinding.imageSubmitButtonNextArrow.visibility = VISIBLE
            }

        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    override fun setFragmentManager(manager: FragmentManager) {

    }

    override fun setOnActionClickListener(listener: (Int) -> Unit) {

    }

    override fun setTitle(title: String) {
        cwsbText = title
        mViewBinding.textSubmitButton.text = (cwsbText)
    }

    override fun setDetail(detail: String) {

    }

    override fun setContent(content: String) {

    }

    override fun setHint(hint: String) {

    }

    override fun setDetailColor(detailColor: Int) {

    }

    override fun setTitleColor(titleColor: Int) {

    }

    fun setStateBan(ban: Boolean) {
        if (ban) {
            mViewBinding.rlSubmitButtonRoot.setBackgroundResource(R.drawable.ai_submit_ban)
        } else {
            mViewBinding.rlSubmitButtonRoot.setBackgroundResource(cwsbBackgroundRes)
        }
    }

}